package org.correct.correcter;

import net.sourceforge.pinyin4j.PinyinHelper;  
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinChange {

	public static String changeToPinYin(String word)
	{
		if((word == null)||(word.trim().equalsIgnoreCase("")))
		{
			return null;
		}
		char [] words = word.toCharArray();
		
		HanyuPinyinOutputFormat hFormat = new HanyuPinyinOutputFormat();
		
		hFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		hFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		hFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		
		String[][] temp = new String[word.length()][];
		for(int i = 0; i < word.length(); i++)
		{
			char c = words[i];
			//System.out.println(c);
			if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+"))
			{
				try 
				{
					temp[i] = PinyinHelper.toHanyuPinyinStringArray(words[i], hFormat); 
				}
				catch (BadHanyuPinyinOutputFormatCombination e)
				{
					System.out.println(e);
				}
			}
			//System.out.println(temp[i][0]);
		}
		String result = "";
		for(int i = 0; i < word.length(); i++)
		{
			if(temp[i]!=null)
				result += temp[i][0];
		}
		return result;
		
	}
}
